package com.aco.party.service.system;

import com.aco.party.dao.custom.DictDao;
import com.aco.party.enums.VisitTypeEnum;
import com.aco.party.model.po.Dict;
import com.aco.party.model.po.Visit;
import com.aco.party.model.vo.DictParam;
import com.aco.party.model.vo.DictVo;
import com.aco.party.model.vo.VisitParam;
import com.aco.party.model.vo.common.PageParam;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by flying on 3/10/2016 0010.
 */
@Service
public class DictService {
    @Resource
    private DictDao dictDao;

    @Resource
    private VisitService visitService;

    public List<DictVo> getByPage(PageParam pageParam,DictParam param) {
        List<DictVo> list = new ArrayList<>();
        List<Dict> dicts = dictDao.getList(pageParam,param);
        DictVo vo;
        if (dicts != null) {
            for (Dict dict : dicts) {
                vo = new DictVo();
                BeanUtils.copyProperties(dict, vo);
                VisitParam visitParam = new VisitParam();
                visitParam.setType(VisitTypeEnum.百科.getValue());
                visitParam.setTargetId(vo.getId());
                vo.setViews(visitService.getCountByParam(visitParam).longValue());

                visitParam = new VisitParam();
                visitParam.setType(VisitTypeEnum.百科点赞.getValue());
                visitParam.setTargetId(vo.getId());
                vo.setLikes(visitService.getCountByParam(visitParam).longValue());
                list.add(vo);
            }
        }
        return list;
    }

    public Integer getTotal(DictParam param) {
        return dictDao.getTotal(param);
    }

    public void update(Dict dict) {
        dict.setUpdateTime(new Date());
        dictDao.updateByPrimaryKeySelective(dict);
    }

    public void add(Dict dict) {
        dict.setCreateTime(new Date());
        dictDao.insertSelective(dict);
    }

    public void delete(Long id) {
        dictDao.deleteByPrimaryKey(id);
    }

    public Dict getByPrimaryKey(Long id) {
        return dictDao.selectByPrimaryKey(id);
    }
}
